package com.easy.project.service.system.impl;

import com.easy.project.base.exception.ServiceException;
import com.easy.project.util.SaTokenUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static com.easy.project.base.result.SystemErrorCodeEnum.ERROR;
import static com.easy.project.base.result.SystemErrorCodeEnum.ERROR_INFO;

/**
 * <p>
 * 鉴权 基础服务类
 * </p>
 *
 * @author wxz
 * @since 2024-01-10
 */
@Service
public class BaseAuthServiceImpl {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 缓存 rsk 默认三十秒
     */
    protected void cacheRsk(SaTokenUtils.RsaKey rsaKey) {
        redisTemplate.opsForValue().set(rsaKey.getPublicKey(), rsaKey.getPrivateKey(), 30, TimeUnit.MINUTES);
    }

    /**
     * 根据公钥获取私钥
     * @param publicKey 公钥
     */
    protected String getCachePrivateKey(String publicKey){
        String privateKey = redisTemplate.opsForValue().get(publicKey);
        if (StringUtils.isBlank(privateKey)){
            throw new ServiceException(ERROR_INFO,"公钥已过期!");
        }
        return privateKey;
    }

}
